package com.briup.es.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.es.auth.SecurityUserContext;
import com.briup.es.client.BookClient;
import com.briup.es.config.exception.ServiceException;
import com.briup.es.config.response.ResultCode;
import com.briup.es.domain.bean.Shopcar;
import com.briup.es.domain.vo.BookVO;
import com.briup.es.domain.vo.ShopCarGoodsVO;
import com.briup.es.service.ShopcarService;
import com.briup.es.mapper.ShopcarMapper;
import com.briup.es.util.BeanUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
@Slf4j
public class ShopcarServiceImpl extends ServiceImpl<ShopcarMapper, Shopcar>
        implements ShopcarService {

    private final BookClient bookClient;

    @Override
    public List<ShopCarGoodsVO> queryList(Integer userId) {
        LambdaQueryWrapper<Shopcar> wrapper = new LambdaQueryWrapper<Shopcar>()
                .eq(Shopcar::getUserId, userId);
        List<Shopcar> shopcars = baseMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(shopcars)) {
            return null;
        } else {
            // 获取到购物车中图书的id
            List<Integer> bookIds = shopcars.stream().map(Shopcar::getBookId).toList();
            List<BookVO> bookVOS = bookClient.queryByIds(bookIds).getData();
            List<ShopCarGoodsVO> goodsVOS = BeanUtil.bean2VOS(shopcars, ShopCarGoodsVO.class);
            // 设置图书相关的数据
            goodsVOS.forEach(goodsVO ->
                    bookVOS.forEach(bookVO -> {
                        if (Objects.equals(bookVO.getId(), goodsVO.getBookId())) {
                            goodsVO.setCover(bookVO.getCover());
                            goodsVO.setName(bookVO.getName());
                            goodsVO.setPrice(bookVO.getPrice());
                        }
                    }));
            return goodsVOS;
        }
    }

    @Override
    public void addShopCar(Integer bookId) {
        BookVO bookVO = bookClient.queryById(bookId).getData();
        if (bookVO == null) {
            throw new ServiceException(ResultCode.BOOK_IS_NOT_EXISTS);
        }
        // 获取当前登录用户id
        Integer userId = SecurityUserContext.currentUserId();
        LambdaQueryWrapper<Shopcar> wrapper = new LambdaQueryWrapper<Shopcar>()
                .eq(Shopcar::getUserId, userId)
                .eq(Shopcar::getBookId, bookId);
        Shopcar shopcar = baseMapper.selectOne(wrapper);
        if (shopcar == null) {
            // 说明该商品未被当前登录用户添加到购物车，直接新增即可
            shopcar = new Shopcar();
            shopcar.setUserId(userId);
            shopcar.setBookId(bookId);
            shopcar.setNum(1);
        } else {
            // 如果不为null，说明该商品已经存在于当前登录用户的购物车中，只需要修改该商品在购物车中的数量即可
            shopcar.setNum(shopcar.getNum() + 1);
        }
        baseMapper.insertOrUpdate(shopcar);
    }

    @Override
    public void updateShopcar(Integer id, Integer num) {
        Shopcar shopcar = baseMapper.selectById(id);
        if (shopcar == null) {
            throw new ServiceException(ResultCode.SHOPCAR_ITEM_IS_NOT_FOUND);
        }
        LambdaUpdateWrapper<Shopcar> wrapper = new LambdaUpdateWrapper<Shopcar>()
                .set(Shopcar::getNum, num)
                .eq(Shopcar::getId, id);
        baseMapper.update(wrapper);
    }

    @Override
    public void deleteShopcar(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        // 继续删除
        baseMapper.deleteByIds(ids);
    }
}